package com.solcoo.common;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import com.solcoo.common.image.DrawableFactory;
import com.solcoo.common.util.CollectionUtil;

public class App {

	private static final String TAG = "App";

	public static Context CONTEXT;

	private static final ArrayList<Activity> activityList = new ArrayList<Activity>();

	public static void init(Application app) {
		CONTEXT = app.getApplicationContext();
		initContant();
	}

	public static class System {
		public static String VERSION; // 当前系统版本
		public static String CHANNEL_ID;// 渠道ID
		public static String CHANNEL_NAME;// 渠道名称
		public static int HEAP_SIZE;// 内存大小
	}

	public static class DISPLAY {
		public static float SCALE;
		public static int SCREEN_WIDTH;
		public static int SCREEN_HEIGHT;
		public static int DEFAULT_IMG;
	}

	/**
	 * 初始化系统变量
	 */
	private static void initContant() {
		ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		App.System.HEAP_SIZE = activityManager.getMemoryClass();
		DrawableFactory.setImageSize(App.System.HEAP_SIZE / 3);
		PackageManager packageManager = getPackageManager();
		Display display = ((WindowManager) getSystemService("window"))
				.getDefaultDisplay();
		try {
			PackageInfo packInfo = packageManager.getPackageInfo(
					getPackageName(), 0);
			ApplicationInfo appInfo = packageManager.getApplicationInfo(
					getPackageName(), PackageManager.GET_META_DATA);

			App.System.CHANNEL_ID = appInfo.metaData.get("channel_id")
					.toString();
			App.System.CHANNEL_NAME = appInfo.metaData
					.getString("channel_name");
			App.System.VERSION = packInfo.versionName;

		} catch (NameNotFoundException e) {
			Log.e(TAG, "onCreate getPackageInfo is fail ", e);
		}
		App.DISPLAY.SCREEN_WIDTH = display.getWidth();
		App.DISPLAY.SCREEN_HEIGHT = display.getHeight();
		App.DISPLAY.SCALE = getResources().getDisplayMetrics().scaledDensity;
		Log.v(TAG, "当前系统信息 内存大小：" + App.System.HEAP_SIZE + "M, 版本号："
				+ App.System.VERSION + ",渠道号:" + App.System.CHANNEL_ID
				+ ",渠道名称:" + App.System.CHANNEL_NAME + ",屏幕宽度："
				+ App.DISPLAY.SCREEN_WIDTH + ",屏幕高度："
				+ App.DISPLAY.SCREEN_HEIGHT + ",SCALE值：" + App.DISPLAY.SCALE);
	}

	public static Object getSystemService(String serviceName) {
		return CONTEXT.getSystemService(serviceName);
	}

	public static Resources getResources() {
		return CONTEXT.getResources();
	}

	public static PackageManager getPackageManager() {
		return CONTEXT.getPackageManager();
	}

	public static String getPackageName() {
		return CONTEXT.getPackageName();
	}

	public static String getString(int resId) {
		return CONTEXT.getString(resId);
	}

	public static String getString(int resId, Object... formatArgs) {
		return CONTEXT.getString(resId, formatArgs);
	}

	public static Drawable getDrawable(int resId) {
		return CONTEXT.getResources().getDrawable(resId);
	}

	public static XmlResourceParser getAnimation(int resId) {
		return CONTEXT.getResources().getAnimation(resId);
	}

	public static File getCacheDir() {
		return CONTEXT.getCacheDir();
	}

	public static AssetManager getAssets() {
		return CONTEXT.getAssets();
	}

	/**
	 * 判断Intent是否存在，是否可用
	 * 
	 * @param context
	 * @param intent
	 * @return
	 */
	public static boolean isIntentAvailable(Intent intent) {
		final PackageManager packageManager = CONTEXT.getPackageManager();
		List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
				PackageManager.GET_ACTIVITIES);
		return CollectionUtil.isNotEmpty(list);
	}

	public static void onCreateActivity(Activity a) {
		activityList.add(a);
	}

	public static void onDestoryActivity(Activity a) {
		activityList.remove(a);
	}

	public static void destory() {
		for (Activity a : activityList) {
			a.finish();
		}
	}
}
